package com.zjh.reactive;

import java.util.concurrent.Flow;

public class ReactiveDemo {
    public static void main(String[] args) {
        Flow.Publisher<Object> publisher=new Flow.Publisher<Object>() {
            @Override
            public void subscribe(Flow.Subscriber<? super Object> subscriber) {
                Flow.Subscription subscription = new Flow.Subscription() {
                    @Override
                    public void request(long n) {
                    // 在publisher 中生成数据，交给subscriber去处理
                        subscriber.onNext("hello world"+n);
                        subscriber.onNext("hello world"+n);
                        subscriber.onComplete();
                    }
                    @Override
                    public void cancel() {
                        System.out.println("取消订阅");
                    }
                };
                subscriber.onSubscribe(subscription);
            }
        };
        Flow.Subscriber<Object> subscriber = new Flow.Subscriber<>() {
            Flow.Subscription subscription;
            @Override
            public void onSubscribe(Flow.Subscription subscription) {
                this.subscription = subscription;
                System.out.println("开始订阅");
                subscription.request(1);
            }

            @Override
            public void onNext(Object item) {
                System.out.println("收到数据：" + item);
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onComplete() {
            }
        };
        publisher.subscribe(subscriber);
    }
}
